package com.qf.demo05.controller;

import com.qf.demo05.service.UserService;
import com.qf.demo05.vo.ResultVo;
import com.qf.demo05.vo.UserRegistVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @PostMapping("/regist")
    public ResultVo regist(UserRegistVo userRegistVo, HttpSession session) throws Exception {

        //0. 打日志
        log.debug("入参，userRegistVo={}",userRegistVo);

        //1. 校验验证码是否正确
        final String sessionCaptcha = (String) session.getAttribute("captcha");

        if (sessionCaptcha == null){
            return ResultVo.error("服务器出故障了，请刷新页面重试");
        }

        if (!sessionCaptcha.equals(userRegistVo.getCaptchar())){
            return ResultVo.error("验证码错误");
        }

        //2. 验证正确，处理用户注册的业务逻辑
        userService.registUser(userRegistVo);

        //3. 如果userService.registUser方法运行时没有抛出异常，则代表注册成功了，返回响应的json

        return ResultVo.ok();

    }

}
